
function basePath(){
    //获取当前网址，如： http://localhost:8080/ems/Pages/Basic/Person.jsp
    var curWwwPath = window.document.location.href;
    //获取主机地址之后的目录，如： /ems/Pages/Basic/Person.jsp
    var pathName = window.document.location.pathname;
    var pos = curWwwPath.indexOf(pathName);
    //获取主机地址，如： http://localhost:8080
    var localhostPath = curWwwPath.substring(0, pos);
    //获取带"/"的项目名，如：/ems
    var projectName = pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    //获取项目的basePath   http://localhost:8080/ems/
    var basePath=localhostPath+projectName+"/";
    return basePath;
};

jQuery(document).ready(function() {
	bindDict("bigClass","DM_XKXMLX","");
	initTable();
	
	
	
	App.setPage("repository");  //Set current page
	App.init(); //Initialise plugins and elements

	//列表查询事件
	$('#search').click(function(){
		$('#repotTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
	});
	//详细页关闭
	$("#modalRepository").on("hidden.bs.modal", function() {
		$('#repotTable').bootstrapTable(('refresh')); 
	});
	//知识收集
	$("#btnAdd").click(function(){
		setModalSize();
    	$('#ifModal').attr("src", "add.html");
		$('#modalTile').html("知识收集");
		$("#modal").modal('show');
	});
	//保存数据事件
	$('#save').click(function(){

		$("#ifModal")[0].contentWindow.save();
		
	});
	$("#bigClass").change(function(){
		var selectBig = $("#bigClass").val();
		console.log(selectBig);
		if(selectBig){
			bindDictForSmallClass("smallClass","REPOSITORY_XL","",selectBig);
		}else{
			$('#smallClass').empty(); 
		}
		
	});
	//清除按钮
	$('#clear').click(function(){
		$("#repotTitle").val("");
		$("#repotKey").val("");
		$("#bigClass").val("");
		$("#smallClass").val("");
	});
	
});	
//js函数
function initTable(){	
	
	$('#repotTable').bootstrapTable({
		method: 'post',
	    url: basePath() + 'repository/list.do',
	    pagination: true, //分页
	    dataType: "json",
        contentType: "application/json",
        pageList: [10, 20, 50, 100],  //可供选择的每页的行数（*）
        striped: true,      //是否显示行间隔色
        sidePagination: "server",
        ////查询参数,每次调用是会带上这个参数，可自定义                         
        queryParams:function(params) {
            return {
                	pageNum: params.offset / params.limit + 1,
                	pageSize: params.limit,
                	repositoryTitle: $('#repotTitle').val(),
                	keyWord: $('#repotKey').val(),
                	bigClass: $('#bigClass').val(),
                	smallClass: $('#smallClass').val()
                };
        },
	    columns: [{  
            field : 'number',  
            title : '序号',  
            align : 'center',
            width : '80px',
            formatter : function(value, row, index) { 
// 		                return index + 1;  
                var page = $('#repotTable').bootstrapTable("getPage");  
                return page.pageSize * (page.pageNumber - 1) + index + 1;  
            }  
        }, {
	        field: 'repositoryCode',
	        title: '知识编号',
	        visible: false
	    }, {
	        field: 'repositoryTitle',
	        title: '知识标题'
	    }, {
	        field: 'bigClassName',
	        title: '知识类别'
	    }, {
	        field: 'smallClass',
	        title: '小类',
	        visible: false
	    }, {
	        field: 'keyWord',
	        title: '关键字'
	    }, {
	        field: 'viewCount',
	        title: '浏览次数'
	    }, {
	        field: 'issuer',
	        title: '发布人'
	    }, {
	        field: 'releaseTime',
	        title: '发布时间'
	    }, {
	        field: 'status',
	        title: '状态',
	        formatter:function(value,row,index){
	        	var result = value;
	        	if(value == "1")
	        		result = "已审核";
	        	else 							
        			result = "未审核";
	        	return result;
	        }
	    }, {
	    	field: 'operate',
            title: '操作',
            align: 'center',
            width: '15%',
            events: operateEvents,
            formatter: operateFormatter
	    }]
	});
}
//详细按钮
function operateFormatter(value, row, index) {
    return [
        '<button type="button" class="edit btn btn-warning  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-edit"></span>编辑</button>',
        '<button type="button" class="delete btn btn-danger  btn-sm" style="margin-right:10px;"><span class="glyphicon glyphicon-minus"></span>删除</button>'
    ].join('');
}
window.operateEvents = {
	'click .delete': function (e, value, row, index) {

		var repositoryPK = row.repositoryPK;
		window.wxc.xcConfirm('是否<strong>删除</strong>知识数据?<br /><span style="color:red;">['+ row.repositoryTitle +']</span>', window.wxc.xcConfirm.typeEnum.confirm,{
			onOk:function(v){
				$.ajax({ 
					url: basePath() + 'repository/delete.do',
					data: {"repositoryPK": repositoryPK},
					dataType: "json",
					success: function(data){	            						
						if(data.result == 1){
				        	$('#repotTable').bootstrapTable(('refresh'));  // 很重要的一步，刷新url！
						}
			      	}
				});
			}
		});
    },
    'click .edit': function (e, value, row, index) {
    	var repositoryPK = row.repositoryPK;
    	
    	setModalSize();
    	
    	$('#ifModal').attr("src", "edit.html?repositoryPK=" + repositoryPK);
    	
		$('#modalTile').html("知识维护");
		$("#modal").modal('show');
    },
};

//ajax获取字典数据
function bindDictForSmallClass(selectID, dictcode, selectdata, bigClass){
	var infoUrl = projectPath + "dict/getDictDetail.do";
		$.ajax({ 
			url: infoUrl,
			data: {
				"dictcode": dictcode,
				"pitemcode": bigClass
			},
			dataType: "json",
			success: function(data){
				if(data.result == 1){   
					var dataStr = data.datas;
					
					// 先清空控件数据
					$('#' + selectID).empty(); 

					var option = $("<option>").val("").text(""); 
					$('#' + selectID).append(option); 
					
					for (var i = 0; i < dataStr.length; i++) {
						var value = dataStr[i].itemcode;
						var text = dataStr[i].itemname;
						
						var option = $("<option>").val(value).text(text); 
						$('#' + selectID).append(option); 
					}	
					
					//选中下拉列表的值
					if(selectdata != ""){
						$('#' + selectID).val(selectdata);
//						$('#' + selectID).find("option[value="+selectdata+"]").prop("selected",true);
					}
				}else{
					$('#' + selectID).empty(); 
				}
	    	}
		});
	
}


